home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
-
- mac_undo.c: Copyright (c) Kevin Hammond 1993. All rights reserved.
-
- This module handles text undo.
-
- *****************************************************************************/
-
- #include "mac.h"
-
- extern char *safemalloc();
-
- int undolen, undostart;
- char *undobuff = NIL;
-
- int keystart = -1;
-
- Boolean pasted = FALSE; /* Set after a paste */
-
- extern Boolean copyscrap;
-
- saveundo(windex)
- int windex;
- {
- TEHandle teh = TEHANDLE(windex);
- char **chs;
-
- if(undobuff != NIL)
- free(undobuff);
-
- TEHLock(windex);
- chs = (char **) (*teh)->hText;
- HLock((Handle)chs);
- undostart = (*teh)->selStart;
- undolen = (*teh)->selEnd-undostart;
-
- /* malloc(0) probably won't work. */
- if(undolen > 0)
- {
- undobuff = safemalloc(undolen);
- strncpy(undobuff,*chs+undostart,undolen);
- }
- else
- undobuff = safemalloc(1);
-
- keystart = -1;
- pasted = FALSE;
- HUnlock((Handle)chs);
- TEHUnlock(windex);
-
- setitem(Menu_Edit,MItem_Undo,"Undo");
- EnableItem(Menu_Edit,MItem_Undo);
- }
-
-
- cantundo()
- {
- if(undobuff != NIL)
- {
- free(undobuff);
- undobuff = NIL;
- setitem(Menu_Edit,MItem_Undo,"Can't Undo");
- DisableItem(Menu_Edit,MItem_Undo);
- keystart = -1;
- pasted = FALSE;
- }
- }
-
-
- undo(windex)
- int windex;
- {
- if(isLegalWindow(windex) && undobuff != NIL)
- {
- TEHandle teh = TEHANDLE(windex);
- int tempundolen, tempundostart;
- char *tempundobuff;
-
- TEHLock(windex);
- tempundostart = keystart < 0 ? (*teh)->selStart : keystart;
- tempundolen = (*teh)->selEnd-tempundostart;
-
- /* Is there enough memory? */
- CheckMemory("Undo",undolen+tempundolen+512);
-
- /* malloc(0) probably won't work. */
- if(tempundolen > 0)
- {
- tempundobuff = safemalloc(tempundolen);
- strncpy(tempundobuff,*((*teh)->hText)+tempundostart,tempundolen);
- }
- else
- tempundobuff = safemalloc(1);
-
- TESetSelect(tempundostart,tempundostart+tempundolen,teh);
- if(windex == worksheet)
- AdjustPromptCount(TRUE);
-
- if(tempundolen > 0)
- TEDelete(teh);
- if(undolen > 0)
- TEInsert(undobuff,undolen,teh);
-
- TESetSelect(tempundostart,tempundostart+undolen,teh);
- if(windex == worksheet)
- AdjustPromptCount(FALSE);
-
- free(undobuff);
- undostart = tempundostart;
- undobuff = tempundobuff;
- undolen = tempundolen;
- TEHUnlock(windex);
-
- copyscrap = FALSE;
- pasted = FALSE;
- keystart = -1;
- }
- }
-
-
- pasteundo(windex)
- int windex;
- {
- saveundo(windex);
- keystart = (*TEHANDLE(windex))->selStart;
- pasted = TRUE;
- }
-
-
- keyundo(windex)
- int windex;
- {
- if(keystart < 0 || pasted)
- {
- saveundo(windex);
- keystart = (*TEHANDLE(windex))->selStart;
- }
- }
-